// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: qjs_navigator.cpp
// Description: Bindings for Navigator
//      Author: Ziming Li
//     Created: 2022-03-30
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./qjs_bindings.h"

#include "blinkit/blink/renderer/bindings/core/qjs/qjs.h"
#include "blinkit/blink/renderer/bindings/core/qjs/qjs_c_function_list_entries.h"
#include "blinkit/blink/renderer/core/frame/Navigator.h"
#ifdef BLINKIT_UI_ENABLED
#   include "bkcommon/bk_strings.h"
#endif

/**
 * https://developer.mozilla.org/en-US/docs/Web/API/Navigator
 */

namespace BlinKit {
namespace qjs {

namespace Strings {
static const char UserAgent[] = "userAgent";
}

#ifdef BLINKIT_CRAWLER_ENABLED
namespace Crawler {

static JSValue GetCookieEnabled(JSContext *ctx, JSValueConst thisVal)
{
    Navigator *navigator = ScriptWrappable::Cast<Navigator>(thisVal);
    return JS_NewBool(ctx, navigator->cookieEnabled());
}

static JSValue GetUserAgent(JSContext *ctx, JSValueConst thisVal)
{
    Navigator *navigator = ScriptWrappable::Cast<Navigator>(thisVal);
    return WrapString(ctx, navigator->userAgent());
}

} // namespace Crawler

JSValue CreateNavigatorPrototypeForCrawler(JSContext *ctx)
{
    static const JSCFunctionListEntry Funcs[] = {
        CGetSetEntry("cookieEnabled", Crawler::GetCookieEnabled, nullptr),
        CGetSetEntry(Strings::UserAgent, Crawler::GetUserAgent, nullptr),
    };

    JSValue ret = JS_NewObject(ctx);
    JS_SetPropertyFunctionList(ctx, ret, Funcs, std::size(Funcs));
    return ret;
}
#endif

#ifdef BLINKIT_UI_ENABLED
JSValue CreateNavigatorPrototypeForUI(JSContext *ctx)
{
    static const JSCFunctionListEntry Funcs[] = {
        CPropStringEntry(Strings::UserAgent, BlinKit::Strings::DefaultUserAgent) // Just a placeholder.
    };

    JSValue ret = JS_NewObject(ctx);
    JS_SetPropertyFunctionList(ctx, ret, Funcs, std::size(Funcs));
    return ret;
}
#endif

} // namespace qjs
} // namespace BlinKit
